package com.aspire.moss.portal.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

import com.aspire.moss.portal.view.CellView.OnDeleteClickListener;

/**
 * 用户整个应用的事件处理
 * 
 * @author Administrator
 * 
 */
public class EventCenter implements OnClickListener, OnLongClickListener,
		OnDeleteClickListener, DragLayer.OnTouchEvent {

	private static final String TAG = EventCenter.class.getSimpleName();

	/**
	 * 当手指在屏幕两边等待多少毫秒后可以移动屏幕
	 */
	private static final short MOVE_WAIT_TIME = 1000;

	/**
	 * 当手指把图标移走了,多少毫秒后重新排列当页图标
	 */
	public static final short ICONGONE_WAIT_TIME = 1000;

	/**
	 * 整个桌面的总布局,当用户长按时拖动图标,将应用加在最上面,并处理
	 */
	private MoveIconController moveIconCtrl;

	/**
	 * 是否长按住了图标,标识下一步的拖动
	 */
	public static boolean isLongClick;

	private Context mContext;

	private ViewController viewController;

	/**
	 * 当用户的手指在屏幕两边时,标志是否启动一个任务监听 当用户把图标移走了,标志是否启动一个监听
	 */
	private boolean startListenterMovePage, startListenerMoveGone;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			initMoveView((CellView) msg.obj);
		}

	};

	public EventCenter(Context mContext, ViewController viewController,
			MoveIconController moveIconController) {
		super();
		this.mContext = mContext;
		this.viewController = viewController;
		this.moveIconCtrl = moveIconController;
	}

	@Override
	public boolean onLongClick(View view) {
		viewController.lockWorkspace();
		/**
		 * 发送消息到handler处理
		 */
		handler.sendMessageDelayed(handler.obtainMessage(0, view), 300);
		return true;
	}

	@Override
	public void onDeleteClick(CellView view) {
		Utils.createDeleteDailog(mContext, viewController, view.appInfo);
	}

	@Override
	public void onClick(View view) {
		Toast.makeText(mContext, "我点了", 50).show();
	}

	@Override
	public boolean onDragLayerTouchEvent(MotionEvent event) {
		Log.d(TAG, "onDragLayerTouchEvent event=" + event);
		final float rawX = event.getX();
		final float rawY = event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			setLongClickViewLocation(rawX, rawY);
			listenerMovePage();
			listenerMoveGone();
			break;
		case MotionEvent.ACTION_UP:
//			 moveLongClickView();
		case MotionEvent.ACTION_CANCEL:
			resetMoveView();
		default:
			break;
		}
		return true;
	}

	/**
	 * 初始化拖动图标
	 */
	private void initMoveView(View longClickView) {
		isLongClick = true;
		moveIconCtrl.initMoveIcon(longClickView);
	}

	/**
	 * 重置拖动变量
	 */
	private void resetMoveView() {
		isLongClick = false;
		moveIconCtrl.resetMoveIcon();
		startListenerMoveGone = false;
	}

	/**
	 * 设置拖动图标的位置
	 * 
	 * @param rawX
	 * @param rawY
	 */
	private void setLongClickViewLocation(float rawX, float rawY) {
		moveIconCtrl.moveIcon(rawX, rawY);
	}

	/**
	 * 移动appview
	 * 
	 * private void moveLongClickView() { if (null != longClickView) { final
	 * AppInfo appInfo = longClickView.appInfo; try { final AppInfo target =
	 * (AppInfo) appInfo.clone(); final int screenId =
	 * viewController.getCurrentScreen(); final CellLayout cellLayout =
	 * viewController.findScreen(screenId); //如果当前的页面跟目标的页面相同,在当页面交换位置
	 * if(target.screen == screenId){ currentPageMove(appInfo, target ,
	 * cellLayout); }else{ final boolean isExists =
	 * cellLayout.isExists(moveIconX, moveIconY , iconWidth , iconHeight); if
	 * (!isExists) { final int [] cellXY = cellLayout.findLastAppView(); if(null
	 * == cellXY) return; target.cellX = cellXY[0]; target.cellY = cellXY[1];
	 * target.screen = screenId; } Log.d(TAG, "move target=" + target); }
	 * 
	 * viewController.moveAppView(appInfo, target); } catch
	 * (CloneNotSupportedException e) { Log.e(TAG, e.getMessage()); } }
	 * 
	 * }
	 */
	/**
	 * 在当页面交换位置
	 */
	private void currentPageMove(AppInfo appInfo, AppInfo target,
			CellLayout cellLayout) {
		// 找到手指松开的地方的cellXY
		final boolean isExists = cellLayout.isExists(moveIconCtrl
				.getMoveIconX(), moveIconCtrl.getMoveIconY(), moveIconCtrl
				.getMoveIconWidth(), moveIconCtrl.getMoveIconHeight());
		if (isExists) {

		} else {
			final int cellXY[] = cellLayout.findLastAppView();
			final int clearUpX = appInfo.cellX;
			final int clearUpY = appInfo.cellY;
			cellLayout.clearUp(clearUpX, clearUpY, true);
			CellView cellView = (CellView) cellLayout.findAppView(appInfo);
			if (null != cellView) {
				cellLayout.moveToXY(cellView, cellXY[0], cellXY[1], false);
				cellView.setVisibility(View.VISIBLE);
			}

		}

	}

	/**
	 * 当用户的手指在屏幕两边的时候 ,看看用户是否是想翻页, 条件是,在1秒以后用户的手指还在这里
	 */
	private void listenerMovePage() {
		Log.i(TAG, "moveIconX=" + moveIconCtrl.getMoveIconX());
		final boolean onLeft = moveIconCtrl.getMoveIconX() < -5 ;
		final int moveIconRight = moveIconCtrl.getMoveIconX() + moveIconCtrl.getMoveIconWidth();
		final boolean onRight = moveIconRight > Utils.getScreenWidth(mContext);
		Log.i(TAG, "onRight" + onRight);
		if (!startListenterMovePage && (onLeft || onRight)) {
			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					Log.d(TAG, "start listener postion run");
					longClickMovePage(moveIconCtrl.getMoveIconX(), moveIconCtrl
							.getMoveIconWidth());
				}

			}, MOVE_WAIT_TIME);
			startListenterMovePage = true;
			Log.d(TAG, "start listener postion");
		}
	}

	/**
	 * 监听用户把图标移走了,当前这个不是为空格吗?,空格后面的控件向前一步走
	 */
	private void listenerMoveGone() {
		if (!startListenerMoveGone) {
		}
	}

	/**
	 * 当长按时 移动屏幕翻页
	 * 
	 * @param x
	 *            当前的坐标
	 * @param width
	 *            图标的宽度
	 */
	private void longClickMovePage(int x, int width) {
		if (x < 0) {
			viewController.scrollRight();
		} else if (x + width > Utils.getScreenWidth(mContext)) {
			if (Workspace.MAX_PAGE - 1 > viewController.getScreens()
					&& viewController.getCurrentScreen() == viewController
							.getScreens() - 1) {
				viewController.addCelllayoutView();
			}
			viewController.scrollLeft();
		}
		startListenterMovePage = false;
	}

	/**
	 * 手指放开时的动画
	 */
	private void longClickUpAnim(CellLayout layout, int cellX, int cellY,
			final CallBack back) {
		final float targetX = layout.getMChildLeft(cellX);
		final float tatgetY = layout.getMChildTop(cellY);
		// moveIconView.setImageBitmap(longClickBitmap);
		TranslateAnimation translateAnimation = new TranslateAnimation(
				moveIconCtrl.getMoveIconX(), moveIconCtrl.getMoveIconY(),
				targetX, tatgetY);
		translateAnimation
				.setAnimationListener(new CellLayout.ViewAnimationListener() {

					@Override
					public void onAnimationEnd(Animation animation) {
						super.onAnimationEnd(animation);
						back.callBack(new Object[] {});
					}

				});
		translateAnimation.start();

	}

}
